from fontTools.ttLib import TTFont

def extract_unicode_from_ttf(ttf_path, output_path):
    # 打开 TTF 字体文件
    font = TTFont(ttf_path)

    # 获取 cmap 表
    cmap = font['cmap'].getcmap(3, 1).cmap

    # 将每个字符的 Unicode 编码写入文件
    with open(output_path, 'w', encoding='utf-8') as f:
        for codepoint in sorted(cmap.keys()):
            f.write(f'{hex(codepoint)}\n')

if __name__ == '__main__':
    ttf_path = 'simsun.ttf'  # 请根据实际路径修改
    output_path = 'text3.txt'
    extract_unicode_from_ttf(ttf_path, output_path)
    print(f'Unicode 编码已写入到 {output_path}')